package com.wu.client.utils;

import java.util.Date;

/**
 * @author Cousin
 * @date 2025/4/1
 * @Description
 */
public class DateUtil {
    // 格式化时间差
    public static String format(Date date) {
        long currentTime = System.currentTimeMillis();
        long timeDifference = currentTime - date.getTime(); // 获取当前时间和输入时间的差值（毫秒）

        // 计算各个时间单位差值
        long diffInSeconds = timeDifference / 1000;
        long diffInMinutes = diffInSeconds / 60;
        long diffInHours = diffInMinutes / 60;
        long diffInDays = diffInHours / 24;
        long diffInMonths = diffInDays / 30; // 近似计算
        long diffInYears = diffInMonths / 12; // 近似计算

        // 判断并返回相应的格式化字符串
        if (diffInSeconds < 60) {
            return "刚刚";
        } else if (diffInMinutes < 2) {
            return "一分钟前";
        } else if (diffInMinutes < 60) {
            return diffInMinutes + "分钟前";
        } else if (diffInHours < 2) {
            return "一小时前";
        } else if (diffInHours < 24) {
            return diffInHours + "小时前";
        } else if (diffInDays < 2) {
            return "一天前";
        } else if (diffInDays < 7) {
            return diffInDays + "天前";
        } else if (diffInMonths < 2) {
            return "一周前";
        } else if (diffInMonths < 12) {
            return diffInMonths + "月前";
        } else if (diffInYears < 2) {
            return "一年前";
        } else {
            return diffInYears + "年前";
        }
    }
}
